package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.UserCityServerCentreExtraAwardLogDao;
import com.sc.nft.entity.UserCityServerCentreExtraAwardLog;
import com.sc.nft.entity.vo.AdminUserCityServerCentreExtraAwardLogDataBaseVO;
import com.sc.nft.entity.vo.AdminUserCityServerCentreExtraAwardLogPageListVO;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.UserCityServerCentreExtraAwardLogService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserCityServerCentreExtraAwardLogServiceImpl implements UserCityServerCentreExtraAwardLogService {

    private final UserCityServerCentreExtraAwardLogDao userCityServerCentreExtraAwardLogDao;

    @Override
    public void add(UserCityServerCentreExtraAwardLog userCityServerCentreExtraAwardLog) {
        userCityServerCentreExtraAwardLog.setCreateId(SysMemberPool.getMember().getId());
        userCityServerCentreExtraAwardLog.insert();
    }

    @Override
    public Page<AdminUserCityServerCentreExtraAwardLogPageListVO> getAdminPageList(PageRequest pageRequest) {
        List<AdminUserCityServerCentreExtraAwardLogPageListVO>list=new ArrayList<>();
        Page<AdminUserCityServerCentreExtraAwardLogDataBaseVO> adminPageList =
                userCityServerCentreExtraAwardLogDao.getAdminPageList(pageRequest);

        Map<Date, List<AdminUserCityServerCentreExtraAwardLogDataBaseVO>> collect = adminPageList.getRecords().stream().collect(Collectors.groupingBy(AdminUserCityServerCentreExtraAwardLogDataBaseVO::getConvertDate));
        collect.forEach((date, adminUserCityServerCentreExtraAwardLogDataBaseVOS) -> {
            AdminUserCityServerCentreExtraAwardLogPageListVO adminUserCityServerCentreExtraAwardLogPageListVO = new AdminUserCityServerCentreExtraAwardLogPageListVO();
            adminUserCityServerCentreExtraAwardLogPageListVO.setDate(date);
            adminUserCityServerCentreExtraAwardLogDataBaseVOS.sort((m1, m2) -> m2.getCreateTime().compareTo(m1.getCreateTime()));
            adminUserCityServerCentreExtraAwardLogPageListVO.setGroupData(adminUserCityServerCentreExtraAwardLogDataBaseVOS);
            list.add(adminUserCityServerCentreExtraAwardLogPageListVO);
        });
        Page<AdminUserCityServerCentreExtraAwardLogPageListVO>data=new Page<>();
        BeanUtil.copyProperties(adminPageList,data);
        data.setRecords(list);
        return data;
    }

}
